package com.wuxianedu.volley;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;

/**
 * Created by TED on 2016/10/3.
 */
public class MyActivity extends AppCompatActivity {

    private ImageView iv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        init1();
        init2();
    }

    /**
     * 使用com.android.volley.toolbox.NetworkImageView 加载网络图片
     */
    private void init2() {
        String url = "http://img31.mtime.cn/pi/2015/02/22/161601.20977088_1000X1000.jpg";
        //找到NetworkImageView 对象
        NetworkImageView niv = (NetworkImageView) findViewById(R.id.net_work_iv);

        ImageLoader loader = MyVolley.getInstance(this).getImageLoader();

        niv.setImageUrl(url,loader);
    }

    /**
     * 使用volley.toolbox.ImageLoader 加载网络图片
     */
    public void init1(){
        iv = (ImageView) findViewById(R.id.iv);

        String url = "http://img31.mtime.cn/pi/2015/09/09/090223.28887177_1000X1000.jpg";
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                iv.setImageBitmap(response);
            }
        }, 1080, 1920, ImageView.ScaleType.CENTER_CROP, null,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        iv.setImageResource(R.mipmap.ic_launcher);
                    }
                });
        MyVolley.getInstance(this).addToQueue(request);
    }
}
